package com.wuwei.elearning.question.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwei.elearning.exception.ElearningMysqlException;
import com.wuwei.elearning.question.domain.Subject;
import com.wuwei.elearning.question.domain.SubjectTest;
import com.wuwei.elearning.question.mapper.QuestionMapper;
import com.wuwei.elearning.question.mapper.SubjectMapper;
import com.wuwei.elearning.question.mapper.SubjectTestMapper;
import com.wuwei.elearning.question.mapper.SubjectTestRelationQuestionMapper;
import com.wuwei.elearning.question.service.SubjectTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课后考试表 服务实现类
 * </p>
 *
 * @author wuwei
 * @since 2025-03-12
 */
@Service
public class SubjectTestServiceImpl extends ServiceImpl<SubjectTestMapper, SubjectTest> implements SubjectTestService {

    @Resource
    private SubjectTestMapper subjectTestMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private SubjectTestRelationQuestionMapper subjectTestRelationQuestionMapper;
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 向subject_test表插入数据，然后再批量向subject_test_relation_question表插入数据
     * @param name
     * @param subjectId
     * @param number
     * @param account
     */
    @Override
    @Transactional
    public Long insert(String name, Long subjectId, Integer number, Long account,String subjectName) {
        //根据number和subjectId从question表中随机选取一定数量的题目
        List<Long> questionIds = questionMapper.subjectRandomTest(subjectId,number);

        if (questionIds.size() < number) {
            number = questionIds.size();
        }
        SubjectTest subjectTest = new SubjectTest();
        subjectTest.setName(name);
        subjectTest.setSubjectId(subjectId);
        subjectTest.setAccount(account);
        subjectTest.setTestNum(number);
        subjectTest.setCreatedTime(LocalDateTime.now());
        subjectTest.setTestTime(number*60);
        subjectTest.setSubjectName(subjectName);
        subjectTestMapper.insert(subjectTest);

        if (!questionIds.isEmpty()) {
            subjectTestRelationQuestionMapper.myBatchInsert(subjectTest.getId(),questionIds);
            return subjectTest.getId();
        } else {
            throw new ElearningMysqlException(30044,"该科目还没有设置题目，请尽快联系管理员");
        }
    }


    /**
     * 根据subjectTest表的主键修改subjectTest表中数据的状态
     * @param score
     * @param subjectTestId
     * @param timestamp
     * @return
     */
    @Override
    public String submit(Integer score, Long subjectTestId, Integer usedTime,Long timestamp) {
        // 将时间戳转换为 Date 或 LocalDateTime
        // 或者转换为 LocalDateTime
        LocalDateTime dateTime = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(timestamp),
                ZoneId.systemDefault()
        );
        SubjectTest subjectTest = subjectTestMapper.selectById(subjectTestId);
        subjectTest.setScore(score);
        subjectTest.setEndTime(dateTime);
        Long subjectId = subjectTest.getSubjectId();
        subjectTest.setTested(1);
        //根据subjectId查询subject表中该科目通过分数是多少
        Subject subject = subjectMapper.selectById(subjectId);
        Integer passedScore = subject.getPassedScore();
        //如果得分超过subject表中设定的分数，则表示通过，否则没通过
        subjectTest.setPassed(score>passedScore?1:0);
        Duration duration = Duration.ofSeconds(usedTime);
        // 使用Duration对象创建LocalTime对象
        LocalTime localTime = LocalTime.MIDNIGHT.plus(duration);
        subjectTest.setUsedTime(localTime);
        subjectTestMapper.updateById(subjectTest);
        if (score>passedScore) {
            return "考试通过";
        } else {
            return "考试未通过";
        }
    }

}
